package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.model.User;
import com.util.ZipUtil;

@Controller
@RequestMapping("/backup")
public class BackupController {
	
	@RequestMapping(value = "/head")
	@ResponseBody
	public Map<String, Object> backup(HttpServletRequest  request, HttpSession session){
		User user = (User) session.getAttribute("user");
		if(!user.getName().equals("yubchen")){
			Map result = new HashMap();
			result.put("info", "请登录");
			return result;
		}
		String path = System.getProperty("webapp.root");
		ZipUtil zipUtil = new ZipUtil(path+"backup/head.zip");
		zipUtil.compress(path+"head");
		Map result = new HashMap();
		result.put("path", "http://wenwen.jelastic.servint.net/backup/head.zip");
		result.put("webroot", path);
		return result;
	}
	
	@RequestMapping(value = "/lucene")
	@ResponseBody
	public Map<String, Object> lucene(HttpServletRequest  request, HttpSession session){
		User user = (User) session.getAttribute("user");
		if(!user.getName().equals("yubchen")){
			Map result = new HashMap();
			result.put("info", "请登录");
			return result;
		}
		String path = System.getProperty("webapp.root");
		ZipUtil zipUtil = new ZipUtil(path+"backup/lucene.zip");
		zipUtil.compress(path+"WEB-INF/lucene");
		Map result = new HashMap();
		result.put("webroot", path);
		result.put("path", "http://wenwen.jelastic.servint.net/backup/lucene.zip");
		return result;
		
	}
	
	@RequestMapping(value = "/upload")
	@ResponseBody
	public Map<String, Object> upload(HttpServletRequest  request, HttpSession session){
		User user = (User) session.getAttribute("user");
		if(!user.getName().equals("yubchen")){
			Map result = new HashMap();
			result.put("info", "请登录");
			return result;
		}
		String path = System.getProperty("webapp.root");
		ZipUtil zipUtil = new ZipUtil(path+"backup/upload.zip");
		zipUtil.compress(path+"upload");
		Map result = new HashMap();
		result.put("webroot", path);
		result.put("path", "http://wenwen.jelastic.servint.net/backup/upload.zip");
		return result;
		
	}
	
	@RequestMapping(value = "/all")
	@ResponseBody
	public Map<String, Object> all(HttpServletRequest  request, HttpSession session){
		User user = (User) session.getAttribute("user");
		if(!user.getName().equals("yubchen")){
			Map result = new HashMap();
			result.put("info", "请登录");
			return result;
		}
		String path = System.getProperty("webapp.root");
		ZipUtil zipUtil1 = new ZipUtil(path+"backup/head.zip");
		zipUtil1.compress(path+"head");
		
		ZipUtil zipUtil2 = new ZipUtil(path+"backup/lucene.zip");
		zipUtil2.compress(path+"WEB-INF/lucene");
		
		ZipUtil zipUtil3 = new ZipUtil(path+"backup/upload.zip");
		zipUtil3.compress(path+"upload");
		Map result = new HashMap();
		result.put("webroot", path);
		result.put("path1", "http://wenwen.jelastic.servint.net/backup/head.zip");
		result.put("path2", "http://wenwen.jelastic.servint.net/backup/lucene.zip");
		result.put("path3", "http://wenwen.jelastic.servint.net/backup/upload.zip");
		return result;
		
	}
}
